package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IPauseOrResetEngineeringOrderView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PhotoBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.customview.PhotoView;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PauseOrResetEngineeringOrderPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by tony on 2018/4/5.
 */

public class ViewEngineeringInstallOrderDetailActivity extends Activity implements View.OnClickListener, IEngineeringInstallOrderDetailView, IPauseOrResetEngineeringOrderView {
    //, PhotoView.OnPhotoViewClickListener, BottomListDialog.OnItemDialogClick,
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private TextView tv_receive_people_colon,tv_reception_time_colon;
    private TextView edt_engineering_address, edt_install_cycle, edt_dispath_order_person_colon, edt_dispath_order_time_colon, edt_receive_people_colon, edt_reception_time_colon;
    private View rl_project;
    //    private PhotoView photoview;
    private ListView lv;
    private LinearLayout ll_engineering_info_content;
    private ImageView img_project_select,img_information_select;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = ViewEngineeringInstallOrderDetailActivity.class.getSimpleName();
    EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    OrderParentBean orderParentBean;
    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
    int photoClickPositon = -1;
    //    private String filePath;
//    List<PhotoBean> photoBeanList;
    private GridView gridView;
    private GridAdapter gridAdapter;
    private List<String> serverUrlList = new ArrayList<>();
    private View ll_btn;
    private TextView btn_left, btn_right;
    private boolean LEFT_CLICKABLE = false, RIGHT_CLICKABLE = false;
    private PauseOrResetEngineeringOrderPresenter pauseOrResetEngineeringOrderPresenter;
    private boolean hasPauseOrder;
    private String orderId = "";
    private CommonEnum.EngineeringORderCategory type;

    private String status;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_view_engineering_install_order_detail);


        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);

        init();

        gridView = findViewById(R.id.gridView);
        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(this);
        gridAdapter.setData(serverUrlList);
        gridView.setAdapter(gridAdapter);


//        photoBeanList = new ArrayList<>();
//        PhotoBean photoBean = new PhotoBean();
//        photoBean.setShowPhoto(true);
//        photoBean.setPhotoType(PhotoBean.DEFAULT_PHOTO);
//        photoBeanList.add(photoBean);

//        photoview.setData(this, photoBeanList);
//        photoview.setPhotoViewClickListener(this);


        Intent mIntent = getIntent();
        if (null != mIntent) {
            String tag = mIntent.getStringExtra(ConstantUtil.KEY_1);

            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("SendOrderForSuperActivity") || tag.equals("DispatchEngineeringInstallationOrderActivity")) {
                    engineeringInstallationOrderBean = (EngineeringInstallationOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    ll_btn.setVisibility(View.GONE);
                } else if (tag.equals("EngineeringInstallStatusTableForManagerActivity1")) {
                    EngineeringInstallStatuBean engineeringInstallStatuBean = (EngineeringInstallStatuBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    if (null != engineeringInstallStatuBean)
                        orderId = engineeringInstallStatuBean.getOrderId();

                    type = CommonEnum.EngineeringORderCategory.INSTALL;

                   // ll_btn.setVisibility(View.VISIBLE);

                    pauseOrResetEngineeringOrderPresenter = new PauseOrResetEngineeringOrderPresenter(this);
                    engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();

                } else{
                    orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    if (null != orderParentBean){
                        orderId = orderParentBean.getId();
                        type = orderParentBean.getCategory();
                    }


                    CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
                    if (CommonEnum.FeedBackState.PAUSE == feedBackState || CommonEnum.FeedBackState.COMPLETE == feedBackState) {
                        ll_btn.setVisibility(View.GONE);
                    } else {
                       // ll_btn.setVisibility(View.VISIBLE);
                    }


                    pauseOrResetEngineeringOrderPresenter = new PauseOrResetEngineeringOrderPresenter(this);
                    engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();

                }
            }




            initData();
        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        edt_engineering_address = findViewById(R.id.edt_engineering_address);
        edt_install_cycle = findViewById(R.id.edt_install_cycle);
        edt_dispath_order_person_colon = findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = findViewById(R.id.edt_dispath_order_time_colon);
        edt_receive_people_colon = findViewById(R.id.edt_receive_people_colon);
        edt_reception_time_colon = findViewById(R.id.edt_reception_time_colon);
        tv_receive_people_colon=findViewById(R.id.tv_receive_people_colon);
        tv_reception_time_colon=findViewById(R.id.tv_reception_time_colon);
//        photoview = findViewById(R.id.photoview);
        ll_btn = findViewById(R.id.ll_btn);
        btn_left = findViewById(R.id.btn_left);
        btn_right = findViewById(R.id.btn_right);

        lv = findViewById(R.id.lv);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        img_project_select = findViewById(R.id.img_project_select);
        img_information_select=findViewById(R.id.img_information_select);

        rl_project = findViewById(R.id.rl_project);

        tv_title.setText(getResources().getText(R.string.engineering_installation));
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_information_select.setBackgroundResource(R.mipmap.arrow_down_icon);

        rl_back.setOnClickListener(this);
        img_project_select.setOnClickListener(this);
        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);
        img_information_select.setOnClickListener(this);


        adapter = new ProductInformationAdapter(this, TAG);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);

        Intent intent=getIntent();
        status=intent.getStringExtra("type");
        if ("BackEngineeringOrderFragment".equals(status)){
            tv_receive_people_colon.setText("退  回  人：");
            tv_reception_time_colon.setText("退回时间：");
        }

    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText(" / " + engineeringInstallationOrderBean.getEngineeringName());
            edt_engineering_address.setText(engineeringInstallationOrderBean.getEngineeringAddress());
//            String beginDate = DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getBeginDate());
            String finishDate = DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getCompleteDate());
//            if (null == beginDate || TextUtils.isEmpty(beginDate))
//                beginDate = "~";

            if (null == finishDate || TextUtils.isEmpty(finishDate))
                finishDate = "~";

            edt_install_cycle.setText(finishDate);
            edt_dispath_order_person_colon.setText(engineeringInstallationOrderBean.getCreateOrderAccountName() );
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));
            String assignReceiveAccountId = engineeringInstallationOrderBean.getAssignReceiveAccountId();
            if(null !=assignReceiveAccountId && !TextUtils.isEmpty(assignReceiveAccountId) && !"null".equals(assignReceiveAccountId))
            edt_receive_people_colon.setText(engineeringInstallationOrderBean.getAssignReceiveAccountName());
            edt_reception_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getAssignReceiveOrderTime()));

            List<String> list = engineeringInstallationOrderBean.getPicUrl();
            if (null != list) {
                serverUrlList.clear();
                serverUrlList.addAll(list);
                gridAdapter.setData(serverUrlList);
                gridAdapter.notifyDataSetChanged();
            }

//            CommonEnum.EngineeringOrderStatus status = engineeringInstallationOrderBean.getStatus();
//            if (null != status) {
//                if (status == CommonEnum.EngineeringOrderStatus.CREATED) {
//                    edt_order_statuse_colon.setText("[创建/未编辑]");
//                } else if (status == CommonEnum.EngineeringOrderStatus.EDITED) {
//                    edt_order_statuse_colon.setText("[已编辑/未派发]");
//                } else if (status == CommonEnum.EngineeringOrderStatus.SEND) {
//                    edt_order_statuse_colon.setText("[派发中]");
//                } else if (status == CommonEnum.EngineeringOrderStatus.RECEIVED) {
//                    edt_order_statuse_colon.setText("[已经接收]");
//                } else if (status == CommonEnum.EngineeringOrderStatus.RETURNED) {
//                    edt_order_statuse_colon.setText("[退回]");
//                } else if (status == CommonEnum.EngineeringOrderStatus.COMPLETED) {
//                    edt_order_statuse_colon.setText("[竣工");
//                }
//            }
//            CommonEnum.AssignStatus assignStatus = engineeringInstallationOrderBean.getAssignStatus();
//            if (null != assignStatus) {
//                if (assignStatus == CommonEnum.AssignStatus.CREATED) {
//                    edt_tv_dispatch_status_colon.setText("[创建/未编辑]");
//                } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
//                    edt_tv_dispatch_status_colon.setText("[已编辑/未派发]");
//                } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
//                    edt_tv_dispatch_status_colon.setText("[派发中]");
//                } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
//                    edt_tv_dispatch_status_colon.setText("[已经接收]");
//                } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
//                    edt_tv_dispatch_status_colon.setText("[退回]");
//                } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
//                    edt_tv_dispatch_status_colon.setText("[竣工");
//                }
//            }
//            if (engineeringInstallationOrderBean.isHasPauseOrder()) {
//                rl_wether_pause_colon.setVisibility(View.VISIBLE);
//                edt_wether_pause_colon.setText("{[暂停]");
//            } else
//                rl_wether_pause_colon.setVisibility(View.GONE);

            if (null != engineeringInstallationOrderBean.getProductList()) {
                productBeanList.addAll(engineeringInstallationOrderBean.getProductList());
                adapter.setData(productBeanList);
                adapter.notifyDataSetChanged();
            }

            if (engineeringInstallationOrderBean.isHasPauseOrder()) {
                hasPauseOrder = true;
                btnClickable(false, true);
            } else {
                hasPauseOrder = false;
                btnClickable(true, false);
            }
        } else if (null != orderParentBean) {
            tv_project_name.setText(orderParentBean.getProjectName());
            tv_engineering_name.setText(" / "+orderParentBean.getEngineeringName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(orderParentBean.getSendOrderTime()));
//            if (orderParentBean.isHasPauseOrder()) {
//                rl_wether_pause_colon.setVisibility(View.VISIBLE);
//                edt_wether_pause_colon.setText("{[暂停]");
//            } else
//                rl_wether_pause_colon.setVisibility(View.GONE);
        }
    }

    // 项目名称/工程名称 收放
    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    // 产品清单 收放
    private void showEngineeringInfoSelect() {
        if (lv.getVisibility() == View.VISIBLE) {
            lv.setVisibility(View.GONE);
            img_information_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv.setVisibility(View.VISIBLE);
            img_information_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }


//    private void showListDialog() {
//        DialogItemBean[] items = new DialogItemBean[]{
//                new DialogItemBean(R.string.camera,
//                        R.layout.dialog_list_item_normal),
//                new DialogItemBean(R.string.gallary,
//                        R.layout.dialog_list_item_normal),
//                new DialogItemBean(R.string.cancel,
//                        R.layout.dialog_list_item_normal)};
//        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
//        dialog.setItemClick(this);
//        dialog.show();
//    }


    private void btnClickable(boolean leftbool, boolean rightbool) {
        LEFT_CLICKABLE = leftbool;
        RIGHT_CLICKABLE = rightbool;

        if (leftbool) {
            btn_left.setClickable(true);
            btn_left.setBackgroundResource(R.mipmap.btn_shor_bg_blue);
        } else {
            btn_left.setClickable(false);
            btn_left.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }

        if (rightbool) {
            btn_right.setClickable(true);
            btn_right.setBackgroundResource(R.mipmap.btn_shor_bg_red);
        } else {
            btn_right.setClickable(false);
            btn_right.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }


    }


    private void pause() {
        btnClickable(false, false);

        hasPauseOrder = true;
        pauseOrResetEngineeringOrderPresenter.pauseOrResetEngineeringOrder();
    }

    private void reset() {
        btnClickable(false, false);

        hasPauseOrder = false;
        pauseOrResetEngineeringOrderPresenter.pauseOrResetEngineeringOrder();


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 项目名称/工程名称 收放
            case R.id.img_project_select:
                showEngineeringInfoDialog();
                break;
                // 产品清单 收放
            case R.id.img_information_select:
                showEngineeringInfoSelect();
                break;
            case R.id.btn_left:  //暂停
                if (LEFT_CLICKABLE)
                    pause();
                break;
            case R.id.btn_right:  //恢复
                if (RIGHT_CLICKABLE)
                    reset();
                break;

        }
    }


//    @Override
//    public void onPhotoViewClick(View view) {
//        int positon = (int) view.getTag();
//        if (positon >= 0 && positon < photoBeanList.size()) {
//            photoClickPositon = positon;
//            showListDialog();
//        }
//
//
//    }
//
//    @Override
//    public void onItemDialogClick(DialogItemBean item) {
//
//        switch (item.getmTextId()) {
//            case R.string.camera: {
//                PhotoUtil.startCamear(this);
//                break;
//            }
//            case R.string.gallary: {
//                PhotoUtil.startGallery(this);
//                break;
//            }
//            default:
//                break;
//        }
//
//    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
//        switch (requestCode) {
//            case PhotoUtil.CAMERA:
////                File picture = new File(PhotoUtil.filePath);
////                if (!picture.exists()) {
////                    picture = new File(PhotoUtil.filePath);
////                }
//
//                if (photoClickPositon >= 0 && photoClickPositon < photoBeanList.size()) {
//                    filePath = PhotoUtil.filePath;
//                    PhotoBean photoBean = photoBeanList.get(photoClickPositon);
//                    photoBean.setPath(filePath);
//                    photoBean.setPhotoType(PhotoBean.URL_PHOTO);
//
//                    photoview.setData(ViewEngineeringInstallOrderDetailActivity.this, photoBeanList);
//
////                    uploadPhotoPresenter.uploadPhtoto();
//
////                    //override(100, 100)
////                    Glide.with(this).load(picture).asBitmap().into(img1);
////                    filePath = PhotoUtil.filePath;
////                    uploadPhotoPresenter.uploadPhtoto();
////                PhotoUtil.startPhotoZoom(AddEngineeringFeedBackActivity.this, Uri.fromFile(picture), PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH);
//
//                }
//
//
//                break;
//
//            case PhotoUtil.GALLERY:
//                if (null != data) {
//                    if (photoClickPositon >= 0 && photoClickPositon < photoBeanList.size()) {
//                        PhotoBean photoBean = photoBeanList.get(photoClickPositon);
//                        photoBean.setUri(data.getData());
//                        photoview.setData(ViewEngineeringInstallOrderDetailActivity.this, photoBeanList);
//                        filePath = data.getData() + "";
////                        uploadPhotoPresenter.uploadPhtoto();
//                    }
////                    Glide.with(this).load(data.getData()).asBitmap().into(img1);
////                    filePath = data.getData() + "";
////                    uploadPhotoPresenter.uploadPhtoto();
//                }
//                break;
//            case PhotoUtil.PHOTORESOULT:
//                break;
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }


    @Override
    public boolean getHasPauseOrder() {
        return hasPauseOrder;
    }

    @Override
    public CommonEnum.EngineeringORderCategory getType() {
//        if (null != orderParentBean)
//            return orderParentBean.getCategory();

        return type;
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getOrderId() {

        return orderId;
    }

    @Override
    public void pauseOrResetOrderSuccessed() {
        if (hasPauseOrder) {
            Toast.makeText(this, "暂停成功", Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(this, "恢复成功", Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    public void pauseOrResetOrderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        if (hasPauseOrder) {
            hasPauseOrder = false;
            btnClickable(true, false);
        } else {
            hasPauseOrder = true;
            btnClickable(false, true);
        }
    }

    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        initData();

    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
